package com.hly.versioncontroladmin.controller;

import com.hly.versioncontroladmin.domain.Bo.VcVersionBo;
import com.hly.versioncontroladmin.domain.Bo.VcVersionChange;
import com.hly.versioncontroladmin.domain.Bo.VersionCheckBo;
import com.hly.versioncontroladmin.domain.VcVersion;
import com.hly.versioncontroladmin.result.PageQuery;
import com.hly.versioncontroladmin.result.Result;
import com.hly.versioncontroladmin.service.VcVersionService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;

@RestController
@RequestMapping("/api/version")
@RequiredArgsConstructor
public class VcVersionController {

    private final VcVersionService versionService;

    @PostMapping("/check")
    public Result check(@RequestBody VersionCheckBo bo){
        return Result.ok(versionService.check(bo));
    }

    @GetMapping("/{id}")
    public Result getById(@PathVariable("id") String id){
        return Result.ok(versionService.getById(id));
    }

    @GetMapping("/list")
    public Result list(PageQuery<VcVersion> pageQuery, VcVersionBo bo){
        return Result.ok(versionService.listQuery(pageQuery, bo));
    }

    @PostMapping("/saveOrUpdate")
    public Result saveOrUpdate(@RequestBody VcVersion version){
        return Result.ok(versionService.saveOrUpdate(version));
    }

    @PostMapping("/change")
    public Result change(@RequestBody VcVersionChange change){
        VcVersion version = versionService.getById(change.getId());
        version.setIsShow(change.getShow());
        return Result.ok(versionService.saveOrUpdate(version));
    }

    @PostMapping("/del/{ids}")
    public Result del(@PathVariable("ids") String ids){
        return Result.ok(versionService.removeBatchByIds(Arrays.asList(ids.split(","))));
    }
}